package util

import (
	"encoding/base64"
	"golang.org/x/crypto/bcrypt"
)

// ValidatePassWd 比对用户密码是否相等
func ValidatePassWd(src string, passWd string) bool {
	bytes, err := base64.StdEncoding.DecodeString(passWd)
	if err != nil {
		return false
	}
	if err := bcrypt.CompareHashAndPassword(bytes, []byte(src)); err != nil {

		return false
	}
	return true
}

// GeneratePassWd 加密密码
func GeneratePassWd(src string) string {
	bytes, err := bcrypt.GenerateFromPassword([]byte(src), bcrypt.DefaultCost)
	if err != nil {
		panic(err)
	}
	return base64.StdEncoding.EncodeToString(bytes)
}
